const API = require('../../../../utils/api')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        form: {
            "areaId": 0,
            "fitmentCase": "",
            "gardenId": 0,
            "houseArea": "",
            "houseImg": "",
            "housingPictures": "",
            "orientation": "",
            "pattern": "",
            "paymentMethod": "",
            "storeyHeight": "",
            "univalenceDay": "",
            "univalenceMonth": ""
        },
        orientationIndex: 0,
        orientationArr: ['东', '南', '西', '北'],
        fitmentCaseIndex: 0,
        fitmentCaseArr: ['精装修', '简单装修', '豪华装修', '毛坯'],
        housingPicturesArr: [],
        areaIndex: 0,
        areaList:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        this.setData({
            ['form.gardenId']: options.gardenId,
        })
        await this.get_back_garden_house_area_list()
        if( options.id ){
            this.setData({
                ['form.id']: options.id,
            })
            await this.get_back_garden_house_detail(options.id)
        }
    },

    /**
     * 接口 - 获取房源详情
     */
    get_back_garden_house_detail:function(id){
        let that = this
        API.get_back_garden_house_detail({
            id: id,
        }).then( res => {
            if( res.code == 200 ){
                let form = that.data.form
                for( let key in res.data ){
                    form[key] = res.data[key]
                }
                //房源图片
                if( form.housingPictures ){
                    that.setData({
                        housingPicturesArr: form.housingPictures.split(',')
                    })
                }
                //下拉选择
                let areaIndex = that.data.areaList.findIndex( ele => ele.id == res.data.areaId ) //面积范围
                console.log('areaIndex', areaIndex)
                let orientationIndex = that.data.orientationArr.findIndex( ele => ele == res.data.orientation ) //朝向
                console.log('orientationIndex', orientationIndex)
                let fitmentCaseIndex = that.data.fitmentCaseArr.findIndex( ele => ele == res.data.fitmentCase ) //装修
                console.log('fitmentCaseIndex', fitmentCaseIndex)
                // console.log(form)

                that.setData({
                    form,
                    areaIndex,
                    orientationIndex,
                    fitmentCaseIndex
                })
            }
        })
    },

    /**
     * 接口 - 获取房源面积列表
     */
    get_back_garden_house_area_list:function(){
        let that = this
        API.get_back_garden_house_area_list().then( res => {
            if( res.code == 200 ){
                let areaList = res.rows.map( ele => {
                    ele.scope += '㎡'
                    return ele
                })
                that.setData({
                    areaList
                })
            }else{
                wx.showToast({
                  title: res.msg,
                })
            }
        })
    },

    /**
     * 事件 - 面积范围选择
     */
    areaChange:function(e){
        let that = this
        this.setData({
            ['form.areaId']: that.data.areaList[parseInt(e.detail.value)],
            areaIndex: parseInt(e.detail.value)
        })
    },

    /**
     * 事件 - 朝向选择
     */
    orientationChange:function(e){
        let that = this
        this.setData({
            ['form.orientation']: that.data.orientationArr[parseInt(e.detail.value)],
            orientationIndex: parseInt(e.detail.value)
        })
    },

    /**
     * 事件 - 装修选择
     */
    fitmentCaseArrChange:function(e){
        let that = this
        this.setData({
            ['form.fitmentCase']: that.data.fitmentCaseArr[parseInt(e.detail.value)],
            fitmentCaseIndex: parseInt(e.detail.value)
        })
    },

    /**
     * 图片上传 - 首图/轮播图
     */
    uploadHouseImg:function(){
        let that = this
        app.uploadImg(1, (data) => {
            that.setData({
                ['form.houseImg']: data
            })
        })
    },

    /**
     * 图片删除 - 首图/轮播图
     */
    deleteHouseImg:function(){
        this.setData({
            ['form.houseImg']: ''
        })
    },

    /**
     * 图片上传 - 房源图片
     */
    uploadHousingPictures:function(){
        let that = this
        app.uploadImg(1, (data) => {
            that.setData({
                housingPicturesArr: [...that.data.housingPicturesArr, ...[data]]
            })
        })
    },

    /**
     * 图片删除 - 房源图片
     */
    deleteHousingPictures:function(e){
        let index = parseInt( e.currentTarget.id )
        let housingPicturesArr =  this.data.housingPicturesArr
        housingPicturesArr.splice(index, 1)
        // console.log(index,housingPicturesArr)
        this.setData({
            housingPicturesArr
        })
    },

    /**
     * 表单提交
     */
    submit:function(e){
        let formData = e.detail.value
        let form = this.data.form
        for( let key in formData ){
            form[key] = formData[key]
        }

        form.areaId = this.data.areaList[this.data.areaIndex].id
        form.orientation = this.data.orientationArr[this.data.orientationIndex]
        form.fitmentCase = this.data.fitmentCaseArr[this.data.fitmentCaseIndex]
        form.housingPictures = this.data.housingPicturesArr.join(',')

        console.log( '表单输入2', form )
        //表单验证
        if( !form.houseArea ){
            wx.showToast({
              title: '请填写面积',
              icon:'none'
            })
            return
        }
        if( !form.univalenceDay ){
            wx.showToast({
              title: '请填写单价',
              icon:'none'
            })
            return
        }
        if( !form.univalenceMonth ){
            wx.showToast({
              title: '请填写月租金',
              icon:'none'
            })
            return
        }
        if( !form.storeyHeight ){
            wx.showToast({
              title: '请填写层高',
              icon:'none'
            })
            return
        }
        if( !form.pattern ){
            wx.showToast({
              title: '请填写格局',
              icon:'none'
            })
            return
        }
        if( !form.houseImg ){
            wx.showToast({
              title: '请上传首图',
              icon:'none'
            })
            return
        }
        if( !form.housingPictures ){
            wx.showToast({
              title: '请上传房源图片',
              icon:'none'
            })
            return
        }

        //编辑
        if( form.id ){ 
            API.update_park_house(form).then( res => {
                if( res.code == 200 ){
                    wx.showToast({
                      title: '修改成功',
                    })
                    setTimeout( () => {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 1500)
                }else{
                    wx.showToast({
                      title: res.msg,
                      icon:'none'
                    })
                }
            })
        }else{
            API.creat_park_house(form).then( res => {
                if( res.code == 200 ){
                    wx.showToast({
                    title: '新增成功',
                    })
                    setTimeout( () => {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 1500)
                }else{
                    wx.showToast({
                    title: res.msg,
                    icon:'none'
                    })
                }
            })
        }
    },
})